package com.sq.universe.base.controller;

import com.sq.universe.base.constants.BaseConstants;
import com.sq.universe.base.utils.AuthUtils;
import com.sq.universe.system.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.web.util.WebUtils;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class BaseController {

    @Autowired
    public RedisSessionDAO redisSessionDAO;

    /**
     * 获取当前用户
     * @return
     */
    public UserEntity  getCurrentUser(){
        return AuthUtils.getCurrentUser();
    }

    /**
     * 获取当前用户
     * @return
     */
    public UserEntity getUserByToken(String token){
        if(StringUtils.isNotBlank(token)){
            SimpleSession simpleSession = (SimpleSession) redisSessionDAO.readSession(token);
            UserEntity userEntity = (UserEntity) simpleSession.getAttribute(BaseConstants.CURRENT_USER);
            return userEntity;
        }
        return null;
    }
}
